Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#585: Avoid fetching attachments when they are already downloaded #654

Merged

Conversation

Sebbo94BY
Copy link
Collaborator

This change implements the suggestion from @yrccondor, which avoids downloading the attachments multiple times, when it's already downloaded. This improves the performance by saving time and reduces the required/used hardware resources.

Code to test it:

<?php
    require_once __DIR__.'/../vendor/autoload.php';

    use PhpImap\Exceptions\ConnectionException;
    use PhpImap\Mailbox;

    $mailbox = new Mailbox(
        '{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
        'some@gmail.com', // Username for the before configured mailbox
        '*********', // Password for the before configured username
        __DIR__.'/files/', // Directory, where attachments will be saved (optional)
        'US-ASCII' // Server encoding (optional)
    );

    try {
        $mail_ids = $mailbox->searchMailbox('ALL');
    } catch (ConnectionException $ex) {
        exit('IMAP connection failed: '.$ex->getErrors('first'));
    } catch (Exception $ex) {
        exit('An error occured: '.$ex->getMessage());
    }

    echo 'Found '.\count($mail_ids)." email(s)...\n";

    $start = microtime(true);
    $attachments_counter_total = 0;

    foreach ($mail_ids as $mail_id) {
        echo "+------ P A R S I N G ------+\n";

        $email = $mailbox->getMail(
            $mail_id, // ID of the email, you want to get
            false // Do NOT mark emails as seen (optional)
        );

        echo 'from-name: '.(string) ($email->fromName ?? $email->fromAddress)."\n";
        echo 'from-email: '.(string) $email->fromAddress."\n";
        echo 'to: '.(string) $email->toString."\n";
        echo 'subject: '.(string) $email->subject."\n";
        echo 'message_id: '.(string) $email->messageId."\n";

        echo 'mail has attachments? ';
        if ($email->hasAttachments()) {
            echo "Yes\n";
        } else {
            echo "No\n";
        }

        if (!empty($email->getAttachments())) {
            $attachments_counter = \count($email->getAttachments());
            $attachments_counter_total += $attachments_counter;

            echo "$attachments_counter attachements\n";
        }
    }

    $mailbox->disconnect();

    echo "Total attachments: $attachments_counter_total\n";
    echo 'Execution time: '.(microtime(true) - $start).' sec.';

Results:

Before After
Found emails (counter) 25 25
Total attachments 26 26
ls -l files/ | grep -Ev "^total" | wc -l 26 26
Execution time (seconds) 18.500550985336 9.8069841861725

Solves #585.

@Sebbo94BY Sebbo94BY merged commit a572a21 into master Jan 8, 2022
@Sebbo94BY Sebbo94BY deleted the 585-Avoid-fetching-attachments-when-already-downloaded branch January 8, 2022 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant